package com.tenebrous.jestic.controller.framwork.meber.author;

import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.author.AuthMenu;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.service.framwork.meber.author.AuthMenuService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.DateUtils;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 菜单管理
 */
@Validated
@RestController
@RequestMapping("/author/menu")
public class MenuController extends SupperController {

    @Autowired
    private AuthMenuService menuService;

    @PostMapping("/get")
    public ApiResult get(@NotBlank String id) {
        AuthMenu item = menuService.selectByPrimaryKey(id);
        if (item != null) {
            return new ApiResult(200, "查询成功。", item);
        }
        return new ApiResult(999, "查询失败，未找到指定对象。");
    }

    /**
     * 查询指定节点及其下属所有节点
     *
     * @return
     */
    @PostMapping("/query-tree")
    public ApiResult queryTree() {
        return new ApiResult(200, "查询成功。", menuService.queryTree());
    }

    /**
     * 查询指定节点下的所有孩子节点
     *
     * @param id 指定节点ID
     * @return
     */
    @PostMapping("/query-child-tree")
    public ApiResult queryTree(@NotBlank String id) {
        return new ApiResult(200, "查询成功。", menuService.queryChildTree(id));
    }

    /**
     * 查询当前登陆用户菜单权限
     *
     * @return
     */
    @PostMapping("/query-rolemenu-tree")
    public ApiResult queryRoleMenuTree(String[] roleids) {
        if (roleids == null) {
            OrgUser ouser = super.getCurrentUser();
            String roleidstr = ouser.getRoleids();
            if (StringUtils.isNotBlank(roleidstr)) {
                roleids = roleidstr.split(",");
                return new ApiResult(200, "查询成功。", menuService.queryRoleMenuTree(roleids));
            }
        }
        return new ApiResult(999, "查询失败。");
    }

    @PostMapping("/query-rolerootmenu")
    public ApiResult queryRoleRootMenu(String[] roleids) {
        if (roleids == null) {
            OrgUser ouser = super.getCurrentUser();
            String roleidstr = ouser.getRoleids();
            if (StringUtils.isNotBlank(roleidstr)) {
                roleids = roleidstr.split(",");
                return new ApiResult(200, "查询成功。", menuService.queryRoleRootMenuTree(roleids));
            }
        }
        return new ApiResult(999, "查询失败。");
    }

    @PostMapping("/query-rolechildmenu")
    public ApiResult queryRoleChildMenu(String[] roleids, String menuid) {
        if (roleids == null) {
            OrgUser ouser = super.getCurrentUser();
            String roleidstr = ouser.getRoleids();
            if (StringUtils.isNotBlank(roleidstr)) {
                roleids = roleidstr.split(",");
                return new ApiResult(200, "查询成功。", menuService.queryRoleChildMenuTree(roleids, menuid));
            }
        }
        return new ApiResult(999, "查询失败。");
    }

    /**
     * 更新对象
     *
     * @param item
     * @return
     */
    @PostMapping("/update")
    @Weblog(description = "更新目录信息", method = WeblogEnum.UPDATE)
    public ApiResult update(@NotNull AuthMenu item) {
        Date dt = new Date();
        OrgUser ouser = super.getCurrentUser();
        if (StringUtils.isBlank(item.getPid()) || "0".equalsIgnoreCase(item.getPid())) {
            item.setPid("0");
            item.setLeve(1);
        }
        if (!"0".equalsIgnoreCase(item.getPid())) {
            AuthMenu pitem = menuService.selectByPrimaryKey(item.getPid());
            if (pitem != null) {
                item.setLeve(pitem.getLeve() + 1);
            } else {
                return new ApiResult(999, "更新失败，未找到指定上级导航。");
            }
        }
        if (StringUtils.isNotBlank(item.getId())) { //修改
            item.setUpdateDate(dt);
            item.setUpdateUserid(ouser.getId());
            menuService.updateSelective(item);
        } else { //新增
            item.setCreateDate(dt);
            item.setId(UUIDUtils.randomUUID());
            item.setCreateUserid(ouser.getId());
            item.setSort(DateUtils.getTimeMillis());
            menuService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。");
    }

    @PostMapping("/update/enabled")
    @Weblog(description = "更新目录状态为启用", method = WeblogEnum.UPDATE)
    public ApiResult updateEnabled(@NotBlank String id) {
        return new ApiResult(200, "更新成功。", menuService.updateEnabled(id, 1));
    }

    @PostMapping("/update/unenabled")
    @Weblog(description = "更新目录状态为禁用", method = WeblogEnum.UPDATE)
    public ApiResult updateUnenabled(@NotBlank String id) {
        return new ApiResult(200, "更新成功。", menuService.updateEnabled(id, 0));
    }

    /**
     * 删除对象
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @Weblog(description = "删除指定目录信息", method = WeblogEnum.DELETE)
    public ApiResult delete(@NotBlank String id) {
        return new ApiResult(200, "删除成功。", menuService.deleteByPrimaryKey(id));
    }

    /**
     * 向上移动
     *
     * @param id
     * @return
     */
    @PostMapping("/up")
    @Weblog(description = "目录上移", method = WeblogEnum.UPDATE)
    public ApiResult up(@NotBlank String id) {
        return menuService.up(id);
    }

    /**
     * 向下移动
     *
     * @param id
     * @return
     */
    @PostMapping("/down")
    @Weblog(description = "目录下移", method = WeblogEnum.UPDATE)
    public ApiResult down(@NotBlank String id) {
        return menuService.down(id);
    }

}
